<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />
<cfset firstProgramYear = "2011-2012" />

<cfset StructDelete(client, "howDidWeDo.programYear") />
<cfset StructDelete(client, "howDidWeDo.programYears") />
<!--- Program_Year --->
<cfif NOT IsDefined("client.howDidWeDo.Program_Year") or resetFilters or NOT IsUserInRole("Super User")>
	<cfset client.howDidWeDo.Program_Year = request.currentProgramYear>
<cfelseif IsDefined("filterProgramYear")>
	<cfset client.howDidWeDo.Program_Year = filterProgramYear>
</cfif>
<cfif client.howDidWeDo.Program_Year neq request.currentProgramYear>
	<cfset isFiltered = true />
	<cfset queryParams.Program_Year = client.howDidWeDo.Program_Year />
<cfelse>
	<cfset queryParams.Program_Year = request.currentProgramYear />
</cfif>

<cfscript>
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(programYears = queryParams.Program_Year, regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""));
	siteGateway = new assets.calgrants.com.gateway.SiteGateway();
	sites = siteGateway.query(programYears = queryParams.Program_Year, regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""));
	workshopGateway = new assets.calgrants.com.gateway.WorkshopGateway();
	workshops = workshopGateway.query(Program_Year = queryParams.Program_Year, regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""));
</cfscript>

<style>
	th.action {width: 110px;}
</style>

<cfoutput>
	<h3>How Did We Do<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>
	<cfif IsUserInRole("Super User")>
		<!--- Menu --->
		<div class="row-fluid" style="margin-bottom: 10px;">
			<div class="span12">
				<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			</div>
		</div>
	</cfif>
	<cfform name="howDidWeDoForm" id="howDidWeDoForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		<cfif IsUserInRole("Super User")>
			<!--- Filters --->
			<div id="filter" class="alert alert-info alert-block" style="display: none;">
				<h4>Filters</h4>
				<table cellpadding="6">
					<tr valign="top">
						<td>
							<label>Program Year</label>
							<cfselect name="filterProgramYear" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
								<cfloop index="y" from="#ListFirst(firstProgramYear, '-')#" to="#ListFirst(request.currentProgramYear, '-')#"><!--- #ListFirst(request.currentProgramYear, '-')# --->
									<cfset py = "#y#-" & y+1>
									<option value="#py#"<cfif queryParams.Program_Year eq py> selected</cfif>>#py#</option>
								</cfloop>
							</cfselect>
						</td>
						<!--- 
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.howDidWeDo.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
								<option value="#Region_ID#"<cfif ListFind(client.howDidWeDo.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>
						<td>
							<label>Organizations</label>
							<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
								<option value="All"<cfif client.howDidWeDo.organizationIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="organizations">
								<option value="#Organization_ID#"<cfif ListFind(client.howDidWeDo.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
								</cfloop>
							</cfselect>
						</td>
						 --->
					</tr>
					<tr>
						<td colspan="5">
							<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=howdidwedo&resetFilters=true" class="btn btn-info">Clear</a>
						</td>
					</tr>
				</table>
			</div>	
		</cfif>
		<!--- Statewide report button --->
		<cfif IsUserInRole("Super User")>
			<p><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Program_Year=#queryParams.Program_Year#" target="_blank" class="btn btn-success">Statewide How Did We Do for #queryParams.Program_Year#</a></p>
		</cfif>
		<!--- Region report button --->
		<cfif IsUserInRole("Regional Admin")>
			<p><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Region_ID=#request.authUser.regionID#&Program_Year=#queryParams.Program_Year#" target="_blank" class="btn btn-success">Region #request.authUser.regionID# How Did We Do for #queryParams.Program_Year#</a></p>
		</cfif>
		<!--- Tabs for Regions, Organizations and Workshops report list --->
		<ul class="nav nav-tabs" id="howDidWeDoTabs">
			<cfif IsUserInRole("Super User")>
				<li class="active"><a href="##regions" data-toggle="tab">Regions</a></li>
			</cfif>
			<li<cfif IsUserInRole('Regional Admin')> class="active"</cfif>><a href="##organizations" data-toggle="tab">Organizations</a></li>
			<li><a href="##workshops" data-toggle="tab">Workshops</a></li>
		</ul>
		<div class="tab-content">
			<!--- Regions --->
			<cfif IsUserInRole("Super User")>
				<div class="tab-pane active" id="regions">
					<table id="regionsTable" class="table table-hover table-bordered">
						<thead>
							<tr>
								<th>Program Year</th>
								<th>Region</th>
								<th>Name</th>
								<th class="action"></th>
							</tr>
						</thead>
						<tbody>
							<cfloop query="regions">
								<tr>
									<td>#queryParams.Program_Year#</td>
									<td>#Region_ID#</td>
									<td>#Region#</td>
									<td>
										<a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Region_ID=#Region_ID#&Program_Year=#queryParams.Program_Year#" target="_blank" class="btn btn-success">View Report</a>
										<!--- <div class="btn-group">
											<a href="##" class="btn btn-success btn-block dropdown-toggle" data-toggle="dropdown">
												Select&nbsp;&nbsp;&nbsp;<span class="caret"></span>
											</a>
											<ul class="dropdown-menu">
												<li><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Region_ID=#Region_ID#" target="_blank">View Report</a></li>
												<li><a href="##" onclick="openShareModal('region', #Region_ID#); return false;">Share Report</a></li>
											</ul>
										</div> --->
									</td>
								</tr>
						</cfloop>
						</tbody>
					</table>
				</div>
			</cfif>
			<!--- Organizations --->
			<div class="tab-pane<cfif IsUserInRole('Regional Admin')> active</cfif>" id="organizations">
				<cfif organizations.RecordCount gt 0>
					<table id="organizationsTable" class="table table-hover table-bordered">
						<thead>
							<tr>
								<th>Program Year</th>
								<th>Organization</th>
								<cfif IsUserInRole("Super User")>
									<th>Region</th>
								</cfif>
								<!--- <th>Initial PY</th> --->
								<th>Workshops</th>
								<th>Participants</th>
								<th class="action"></th>
							</tr>
						</thead>
						<tbody>
							<cfloop query="organizations">
								<cfif queryParams.Program_Year gte Initial_Program_Year and Workshop_Count gt 0>
									<tr>
										<td>#queryParams.Program_Year#</td>
										<td>#Organization#</td>
										<cfif IsUserInRole("Super User")>
											<td>#Region_ID#</td>
										</cfif>
										<!--- <td>#Initial_Program_Year#</td> --->
										<td>#Workshop_Count#</td>
										<td>#Student_Count#</td>
										<td>
											<cfif Student_Count gt 0>
												<a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Organization_ID=#Organization_ID#&Program_Year=#queryParams.Program_Year#" target="_blank" class="btn btn-success">View Report</a>
											<cfelse>
												No participants
											</cfif>
											<!--- <div class="btn-group">
												<a href="##" class="btn btn-success btn-block dropdown-toggle" data-toggle="dropdown">
													Select&nbsp;&nbsp;&nbsp;<span class="caret"></span>
												</a>
												<ul class="dropdown-menu">
													<li><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Organization_ID=#Organization_ID#" target="_blank">View Report</a></li>
													<li><a href="##" onclick="openShareModal('organiation', #Organization_ID#); return false;">Share Report</a></li>
												</ul>
											</div> --->
										</td>
									</tr>
								</cfif>
							</cfloop>
						</tbody>
					</table>
				<cfelse>
					<div class="alert alert-info notFound">No organizations</div>
				</cfif>
			</div>
			<!--- Sites --->
			<!--- <div class="tab-pane" id="sites">
				<cfif sites.RecordCount gt 0>
					<table id="sitesTable" class="table table-hover table-bordered">
						<thead>
							<tr>
								<th>Program Year</th>
								<th>Site</th>
								<th>Organization</th>
								<cfif IsUserInRole("Super User")>
									<th>Region</th>
								</cfif>
								<th>City</th>
								<th>Workshops</th>
								<th class="action"></th>
							</tr>
						</thead>
						<tbody>
							<cfloop query="sites">
								<tr>
									<td>2012-2013</td>
									<td>#Site#</td>
									<td>#Organization#</td>
									<cfif IsUserInRole("Super User")>
										<td>#Region_ID#</td>
									</cfif>
									<td>#City#</td>
									<td>#Workshop_Count#</td>
									<td><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Site_ID=#Site_ID#" target="_blank">View Report</a>
										<!--- <div class="btn-group">
											<a href="##" class="btn btn-success btn-block dropdown-toggle" data-toggle="dropdown">
												Select&nbsp;&nbsp;&nbsp;<span class="caret"></span>
											</a>
											<ul class="dropdown-menu">
												<li><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Organization_ID=#Organization_ID#" target="_blank">View Report</a></li>
												<li><a href="##" onclick="openShareModal('organiation', #Organization_ID#); return false;">Share Report</a></li>
											</ul>
										</div> --->
									</td>
								</tr>
						</cfloop>
						</tbody>
					</table>
				<cfelse>
					<div class="alert alert-info notFound">No sites</div>
				</cfif>
			</div> --->
			<!--- Workshops --->
			<div class="tab-pane" id="workshops">
				<cfif workshops.RecordCount gt 0>
					<table id="workshopsTable" class="table table-hover table-bordered">
						<thead>
							<tr>
								<th>Program Year</th>
								<cfif IsUserInRole("Super User")>
									<th>Region</th>
								</cfif>
								<th>Organization</th>
								<th>Site</th>
								<th>Workshop</th>
								<th>Participants</th>
								<th class="action"></th>
							</tr>
						</thead>
						<tbody>
							<cfloop query="workshops">
								<tr>
									<td>#Program_Year#</td>
									<cfif IsUserInRole("Super User")>
										<td>#Region_ID#</td>
									</cfif>
									<td>#Organization#</td>
									<td>#Site#</td>
									<td>#DateFormat(Workshop_Date, "m/d/yyyy")# #TimeFormat(Start_Time, "h:mmtt")#-#TimeFormat(End_Time, "h:mmtt")#</td>
									<td>#Student_Count#</td>
									<td>
										<cfif Student_Count gt 0>
										<a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Workshop_ID=#Workshop_ID#&Program_Year=#Program_Year#" target="_blank" class="btn btn-success">View Report</a>
										<!--- <div class="btn-group">
											<a href="##" class="btn btn-success btn-block dropdown-toggle" data-toggle="dropdown">
												Select&nbsp;&nbsp;&nbsp;<span class="caret"></span>
											</a>
											<ul class="dropdown-menu">
												<li><a href="http://#cgi.http_host#/assets/calgrants/ui/printable/howDidWeDo.cfm?Organization_ID=#Organization_ID#" target="_blank">View Report</a></li>
												<li><a href="##" onclick="openShareModal('organiation', #Organization_ID#); return false;">Share Report</a></li>
											</ul>
										</div> --->
										<cfelse>
											No participants
										</cfif>
									</td>
								</tr>
							</cfloop>
						</tbody>
					</table>
				<cfelse>
					<div class="alert alert-info notFound">No workshops</div>
				</cfif>
			</div>
		</div>
	</cfform>
</cfoutput>

<!--- <cfif cgi.REMOTE_ADDR eq "186.151.104.20">
	<h2>WMS table column summary</h2>
	<cfoutput>
		<table border="1" cellpadding="10" cellspacing="0">
			<tr valign="top">
				<td>
					<h3>Regions</h3>
					#Replace(regions.columnlist, ",", "<br>", "all")#
				</td>
				<td>
					<h3>Orgs</h3>
					#Replace(organizations.columnlist, ",", "<br>", "all")#
				</td>
				<td>
					<h3>Sites</h3>
					#Replace(sites.columnlist, ",", "<br>", "all")#
				</td>
				<td>
					<h3>Worksohps</h3>
					#Replace(workshops.columnlist, ",", "<br>", "all")#
				</td>
			</tr>
		</table>
	</cfoutput>
</cfif> --->